package com.chianghao.rockframewrok.core.web;

import java.io.Serializable;

import com.chianghao.rockframewrok.core.exception.HaoException;

public class R<T> implements Serializable {

    private static  final long serialVersionUID = 1L;
    private Error error;
    private T       data;
    private boolean status;

    public static <T> R<Object> data() {
        R<Object> r = new R<Object>();
        r.setStatus(true);
        return r;
    }

    public static <T> R<T> data(T data) {
        R<T> r = new R<T>();
        r.setStatus(true);
        r.setData(data);
        return r;
    }

    public static <T> R<Object> error(Throwable e) {
        R<Object> r = new R<Object>();
        r.setStatus(false);
        Error error = new Error();
        String eCode = "999999";
        String eMsg  = e.getMessage();
        if(e instanceof HaoException) {
            eCode = ((HaoException) e).getCode();
        }
        error.setCode(eCode);
        error.setMessage(eMsg);
        r.setError(error);
        return r;
    }

    public static  R<Object> error(String eCode,String eMsg) {
        R<Object> r = new R<Object>();
        r.setStatus(false);
        Error error = new Error();
        error.setCode(eCode);
        error.setMessage(eMsg);
        r.setError(error);
        return r;
    }

    public static  R<Object> error(String eMsg) {
        R<Object> r = new R<Object>();
        r.setStatus(false);
        Error error = new Error();
        String eCode = "999999";
        error.setCode(eCode);
        error.setMessage(eMsg);
        r.setError(error);
        return r;
    }


    public boolean isStatus() {
        return status;
    }

    public void setStatus(boolean status) {
        this.status = status;
    }

    public Error getError() {
        return error;
    }

    public void setError(Error error) {
        this.error = error;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

}
